package top.dark.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dark.product.entity.AttrEntity;
import top.dark.product.entity.ProductAttrValueEntity;
import top.dark.product.service.AttrService;
import top.dark.common.utils.PageUtils;
import top.dark.common.utils.R;
import top.dark.product.vo.AttrRespVo;
import top.dark.product.vo.AttrVo;


/**
 * 商品属性
 *
 * @author lijiao
 * @email DarknessShadow11@outlook.com
 * @date 2021-02-01 10:42:11
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * /product/attr/base/listforspu/{spuId}
     * 获取spu规格
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listAttrsforspu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> attrValues = attrService.listAttrsforspu(spuId);
        return R.ok().put("data", attrValues);
    }


    /**
     * 基本属性分页查询
     */
    @GetMapping("/{attrType}/list/{catId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catId") Long catId,
                          @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.baseAttrListPage(params, catId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
